package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type IotTemplateEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	IotName      string // 云端平台
	SubsetName   string // 子集名称
	TemplateType string // 模板类型
	TemplateName string // 模板名称

	TemplateParam map[string]interface{} // 配置参数
	ExtendParam   map[string]interface{} // 扩展参数
}

type iotTemplatePo struct {
	Id            int64                  `json:"id" db:"id"`                        // 对象id
	CreateTime    int64                  `json:"createTime" db:"create_time"`       // 创建时间
	UpdateTime    int64                  `json:"updateTime" db:"update_time"`       // 更新时间
	IotName       string                 `json:"iotName" db:"iot_name"`             // 云端平台
	SubsetName    string                 `json:"subsetName" db:"subset_name"`       // 模板名称
	TemplateType  string                 `json:"templateType" db:"template_type"`   // 模板名称
	TemplateName  string                 `json:"templateName" db:"template_name"`   // 模板名称
	TemplateParam map[string]interface{} `json:"templateParam" db:"template_param"` // 配置参数
	ExtendParam   map[string]interface{} `json:"extendParam" db:"extend_param"`     // 扩展参数
}

func (e *IotTemplateEntity) Type() string {
	return "IotTemplateEntity"
}

func (e *IotTemplateEntity) TableName() string {
	return "tb_iot_template"
}

func (e *IotTemplateEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *IotTemplateEntity) Instance() *IotTemplateEntity {
	return &IotTemplateEntity{
		BaseEntity:    new(commEntity.BaseEntity),
		TemplateParam: make(map[string]interface{}),
		ExtendParam:   make(map[string]interface{}),
	}
}

func (e *IotTemplateEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *IotTemplateEntity) NewPo() interface{} {
	return &iotTemplatePo{
		TemplateParam: make(map[string]interface{}),
		ExtendParam:   make(map[string]interface{}),
	}
}

func (e *IotTemplateEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *IotTemplateEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.IotName)
	p = append(p, e.SubsetName)
	p = append(p, e.TemplateType)
	p = append(p, e.TemplateName)
	return p
}

func (e *IotTemplateEntity) BuildByJson(jsn *string) error {
	var po iotTemplatePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *IotTemplateEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *IotTemplateEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*iotTemplatePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.TemplateParam = po.TemplateParam
	ptr.ExtendParam = po.ExtendParam
	ptr.IotName = po.IotName
	ptr.SubsetName = po.SubsetName
	ptr.TemplateType = po.TemplateType
	ptr.TemplateName = po.TemplateName
}

func (e *IotTemplateEntity) BuildToPo() interface{} {
	ptr := e
	po := iotTemplatePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.TemplateParam = ptr.TemplateParam
	po.ExtendParam = ptr.ExtendParam
	po.IotName = ptr.IotName
	po.SubsetName = ptr.SubsetName
	po.TemplateType = ptr.TemplateType
	po.TemplateName = ptr.TemplateName

	return &po
}
